package com.es.test;


import com.es.entity.Member;
import com.es.entity.Order;
import com.es.service.MemberService;
import com.es.service.impl.impl.MemberServiceImpl;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述:
 * MyBatis-
 *
 * @author Derek
 * 创建时间：2022-05-09 9:50
 */
public class One2ManyServlet {
    // one
    MemberService service ;
    @Before
    public void before(){
        service  = new MemberServiceImpl();
    }
    @Test
    public  void get(){
        Integer id = 3;
        Member member = service.get(id);
        System.out.println(member);
        System.out.println("他的订单数是:"+member.getOrders().size());
    }
    @Test
    public void save(){
        Member member = new Member();
        member.setName("小康");
        member.setPhone("18615612345");
        //
        List<Order> orders = new ArrayList<>();
        //订单
        Order o1 = new Order();
        o1.setMember(member);
        o1.setTitle("Dell电脑");
        Order o2 = new Order();
        o2.setTitle("华为手机");
        o2.setMember(member);
        Order o3 = new Order();
        o3.setTitle("Ipad");
        o3.setMember(member);
        orders.add(o1);
        orders.add(o2);
        orders.add(o3);

        member.setOrders(orders);

        service.save(member);
    }

    @Test
    public void list(){
        List<Member> members = service.list();
        /*members.forEach (m->{
            System.out.println("当前会员："+m.getName()+"，联系电话"+m.getPhone());
            //遍历当前会员的订单
            System.out.println("订单号:\t订单标题");
            m.getOrders().forEach(o->{
                System.out.println(o.getId()+"\t"+o.getTitle());
            });
        });*/
        System.out.println("会员ID\td会员姓名\t会员电话");
        members.forEach(m->{
            System.out.println(m.getId()+"\t"+m.getName()+"\t"+m.getPhone());
            //
            System.out.println("\t我的订单信息");
            List<Order> orders = m.getOrders();
            System.out.println("\t订单号\t订单标题");
            orders.forEach(o->{
                System.out.println("\t"+o.getId()+"\t"+o.getTitle());
            });

        });
    }

}
